package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.bos.web.action.utils.PinYin4jUtils;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class AreaAction extends BaseAction<Area> {

	
	@Autowired
	private AreaService areaService;
	 private File file;

	public void setFile(File file) {
		this.file = file;
	}
	
	@Action(value="upload_file")
	public String uploadFile() throws Exception{
		List<Area> areas=new ArrayList<>();
		HSSFWorkbook hssfWorkbook=new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		for (Row row : sheet) {
			if(row.getRowNum()==0){
				continue;
			}
			if(row.getCell(0)==null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;
			}
			
		Area area=new Area();
		
		area.setId(row.getCell(0).getStringCellValue());
		area.setProvince(row.getCell(1).getStringCellValue());
		area.setCity(row.getCell(2).getStringCellValue());
		area.setDistrict(row.getCell(3).getStringCellValue());
		area.setPostcode(row.getCell(4).getStringCellValue());
		
		
		String province=area.getProvince();
		String city=area.getCity();
		String district=area.getDistrict();
		province = province.substring(0, province.length() - 1);
		city = city.substring(0, city.length() - 1);
		district = district.substring(0, district.length() - 1);
		String[] strings = PinYin4jUtils.getHeadByString(province+city+district);
		StringBuffer sBuffer=new StringBuffer();
		for (String string : strings) {
			sBuffer.append(string);
		}
		
		String shortcode=sBuffer.toString();
		area.setShortcode(shortcode);
		
		String citycode = PinYin4jUtils.hanziToPinyin(city, "");
		area.setCitycode(citycode);
		areas.add(area);
		}
		areaService.save(areas);
		return NONE;
	}
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	@Action(value="area_serach",results={@Result(name="success" ,type="json")})
	public String area_serach(){
		Pageable pageable=new PageRequest(page-1, rows);
		Specification<Area>specification=new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root, 
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=null;
				 List<Predicate>predicates=new ArrayList<Predicate>();
				if(StringUtils.isNoneBlank(model.getProvince())){
					Predicate p1 = cb.like(root.get("province").as(String.class),"%"+model.getProvince()+"%");
					predicates.add(p1);
				}
				if(StringUtils.isNoneBlank(model.getCity())){
					Predicate p2 = cb.like(root.get("city").as(String.class),"%"+model.getCity()+"%");
					predicates.add(p2);
				}
				if(StringUtils.isNoneBlank(model.getDistrict())){
					Predicate p3 = cb.like(root.get("district").as(String.class),"%"+model.getDistrict()+"%");
					predicates.add(p3);
				}
				
				predicate=cb.and((Predicate[]) predicates.toArray(new Predicate[0]));
			
				return predicate;
			}
		};
		Page<Area>pageData=areaService.findAll(specification,pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	@Action(value="area_save",results={@Result(name="success",type="redirect",location="./pages/base/sub_area.html")})
	public String save(){
		areaService.save(model);
		return SUCCESS;
	}
	@Action(value="area_findallarea",results={@Result(name="success" ,type="json")})
	public String findArea(){
		List<Area> list=areaService.findAll();
		System.out.println(list);
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
